package com.siwei.eqi.pojo;


import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
public class ResultBean<T> implements Serializable {
    private static final long serialNumber = 1L;
    @ApiModelProperty("状态码: 200成功/500异常/404参数校验异常/401暂未登录或token已经过期/403没有相关权限")
    private long code;
    @ApiModelProperty("接口消息提示")
    private String msg;
    @ApiModelProperty("返回的数据实体")
    private T data;

    public ResultBean() {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMsg();
    }

    public ResultBean(T data) {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMsg();
        this.data = data;
    }

    public ResultBean(Throwable e) {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMsg();
        this.msg = e.toString();
        this.code = ResultCode.FAILED.getCode();
    }

    public ResultBean(long code, String msg) {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMsg();
        this.code = code;
        this.msg = msg;
    }

    public ResultBean(long code, String msg, T data) {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMsg();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResultBean<T> success() {
        return new ResultBean(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
    }

    public static <T> ResultBean<T> success(T data) {
        return new ResultBean(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    public static <T> ResultBean<T> success(String msg, T data) {
        return new ResultBean(ResultCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> ResultBean<T> failed() {
        return new ResultBean(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg());
    }

    public static <T> ResultBean<T> failed(T data) {
        return new ResultBean(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), data);
    }

    public static <T> ResultBean<T> failed(String msg, T data) {
        return new ResultBean(ResultCode.FAILED.getCode(), msg, data);
    }

    public static <T> ResultBean<T> result(ResultCode code, String msg, T data) {
        return new ResultBean(code.getCode(), msg, data);
    }
}
